/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.feiyue.basic.reader.dao.FileRecordDao;
import com.feiyue.basic.reader.dao.SettingDao;
import com.feiyue.basic.reader.dialog.LoadingDialog;
import com.feiyue.basic.reader.pojo.FileRecord;
import com.feiyue.basic.server.db.ChapterDBHandler;
import com.feiyue.basic.server.parser.Chapter;
import com.feiyue.helper.Runnable2chapterlist;
import com.feiyue.helper.Thread2getchapterlist;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import java.util.ArrayList;
import java.util.List;

public class BookCatologActivity extends Activity implements
		View.OnTouchListener, GestureDetector.OnGestureListener {
	private String author;
	private int bookid;
	private String bookname;
	private int categoryid;
	private String categoryname;
	private CatologUpateMessageReceiver catologupdatemessageReceiver;
	private int chapterNum = 0;
	private List<Chapter> chapterlist;
	private LinearLayout contentlinearlayout;
	private Context context;
	private FileRecordDao fileRecordDao;
	private List<FileRecord> fileRecordlist = new ArrayList();
	protected CharSequence firstlabel = "更新";
	public Handler handler;
	private String introduction;
	private String issuestate;
	private PullToRefreshListView list;
	private LoadingDialog loadingDialog;
	private GestureDetector mGestureDetector;
	private int minVelocity = 150;
	private ImageView profile_header_back;
	private Runnable2chapterlist runnable2chapterlist;
	protected CharSequence secondlabel = "松开立即更新、往回拉则取消";
	private SettingDao settingDao;
	private TextView textViewcount;
	protected CharSequence thirdlabel = "正在更新";
	private Thread2getchapterlist thread2getchapterlist = null;
	private int verticalMinDistance = 200;

	public boolean dispatchTouchEvent(MotionEvent paramMotionEvent) {
		if (!(this.mGestureDetector.onTouchEvent(paramMotionEvent)))
			return super.dispatchTouchEvent(paramMotionEvent);
		return true;
	}

	public void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.catalog);
		this.catologupdatemessageReceiver = new CatologUpateMessageReceiver();
		IntentFilter localIntentFilter = new IntentFilter("CatologUpdate");
		registerReceiver(this.catologupdatemessageReceiver, localIntentFilter);
		this.mGestureDetector = new GestureDetector(this);
		LinearLayout localLinearLayout = (LinearLayout) findViewById(R.id.novel_catalog_view);
		localLinearLayout.setOnTouchListener(this);
		localLinearLayout.setLongClickable(true);
		this.context = getApplicationContext();
		this.loadingDialog = new LoadingDialog(this, R.style.loadingDialog);
		this.loadingDialog.textInfo = "获取最新目录中";
		this.categoryname = getIntent().getExtras().getString("categoryname");
		this.categoryid = getIntent().getExtras().getInt("categoryid");
		this.bookname = getIntent().getExtras().getString("bookname");
		this.bookid = getIntent().getExtras().getInt("bookid");
		this.author = getIntent().getExtras().getString("author");
		this.introduction = getIntent().getExtras().getString("introduction");
		this.issuestate = getIntent().getExtras().getString("issuestate");
		((TextView) findViewById(R.id.novel_name_text)).setText(this.bookname);
		ImageView localImageView = (ImageView) findViewById(R.id.update_novel);
		localImageView.setVisibility(0);
		localImageView.setOnClickListener(new View.OnClickListener() {
			public void onClick(View paramView) {
				BookCatologActivity.this.loadingDialog.show();
				new Thread2getchapterlist(BookCatologActivity.this.context,
						BookCatologActivity.this.bookid,
						BookCatologActivity.this.categoryid,
						BookCatologActivity.this.runnable2chapterlist,
						BookCatologActivity.this.handler, "update").start();
			}
		});
		this.textViewcount = ((TextView) findViewById(R.id.chapter_count_text));
		this.contentlinearlayout = ((LinearLayout) findViewById(R.id.chaptercontent));
		this.contentlinearlayout.setVisibility(8);
		StringBuilder localStringBuilder = new StringBuilder(
				String.valueOf(ChapterDBHandler.myTableName));
		String str1 = ChapterDBHandler.suffix;
		Object[] arrayOfObject = new Object[1];
		arrayOfObject[0] = Integer.valueOf(this.bookid);
		String str2 = localStringBuilder.append(String.format(str1, arrayOfObject)).toString();
		try {
			ChapterDBHandler localChapterDBHandler = new ChapterDBHandler(
					this.context);
			if (!(localChapterDBHandler.IsTableExist(str2)))
				this.loadingDialog.show();
			localChapterDBHandler.destroy();
			this.list = ((PullToRefreshListView) findViewById(R.id.novel_catalog_listview));
			this.list.setMode(Mode.BOTH);
			this.list.setAnimation(null);
			this.list.getLoadingLayoutProxy().setPullLabel(this.firstlabel);
			this.list.getLoadingLayoutProxy().setRefreshingLabel(
					this.thirdlabel);
			this.list.getLoadingLayoutProxy().setReleaseLabel(this.secondlabel);
			this.handler = new Handler();
			this.runnable2chapterlist = new Runnable2chapterlist(this,
					this.textViewcount, this.contentlinearlayout,
					this.loadingDialog, this.list, this.categoryname,
					this.categoryid, this.bookname, this.bookid, this.author,
					this.introduction, this.issuestate);
			this.profile_header_back = ((ImageView) findViewById(R.id.profile_header_back));
			this.profile_header_back
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View paramView) {
							BookCatologActivity.this.finish();
							BookCatologActivity.this.overridePendingTransition(
									R.anim.slide_in_left, R.anim.slide_out_right);
						}
					});
			return;
		} catch (Exception localException) {
			this.loadingDialog.show();
		}
	}

	protected void onDestroy() {
		this.thread2getchapterlist = null;
		unregisterReceiver(this.catologupdatemessageReceiver);
		super.onDestroy();
	}

	public boolean onDown(MotionEvent paramMotionEvent) {
		return false;
	}

	public boolean onFling(MotionEvent paramMotionEvent1,
			MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
		float f = Math.abs(paramMotionEvent1.getX() - paramMotionEvent2.getX())
				/ Math.abs(paramMotionEvent1.getY() - paramMotionEvent2.getY());
		if ((paramMotionEvent1.getX() - paramMotionEvent2.getX() > 0.0F)
				&& (f - 2.5D > 0.0D)) {
			finish();
			overridePendingTransition(2130968587, 2130968588);
			return true;
		}
		if ((paramMotionEvent1.getX() - paramMotionEvent2.getX() < 0.0F)
				&& (f - 2.5D > 0.0D)) {
			finish();
			overridePendingTransition(2130968586, 2130968589);
			return true;
		}
		return false;
	}

	public void onLongPress(MotionEvent paramMotionEvent) {
	}

	protected void onNewIntent(Intent paramIntent) {
		super.onNewIntent(paramIntent);
	}

	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	protected void onResume()
    {
        if (thread2getchapterlist != null) {
            fileRecordDao = new FileRecordDao(this);
            settingDao = new SettingDao(this);
            fileRecordlist = fileRecordDao.getList(bookid);
            if (fileRecordlist.size() > 0)
                chapterNum = ((FileRecord)fileRecordlist.get(0)).getChapterNum();
            fileRecordDao.close();
            settingDao.close();
            if (chapterNum != runnable2chapterlist.getChapterNum())
                handler.post(runnable2chapterlist);
        } else {
            thread2getchapterlist = new Thread2getchapterlist(context, bookid, categoryid, runnable2chapterlist, handler, "cache first");
            thread2getchapterlist.start(); 
        }
        
        super.onResume();
        MobclickAgent.onResume(this);
    }

	public boolean onScroll(MotionEvent paramMotionEvent1,
			MotionEvent paramMotionEvent2, float paramFloat1, float paramFloat2) {
		return false;
	}

	public void onShowPress(MotionEvent paramMotionEvent) {
	}

	public boolean onSingleTapUp(MotionEvent paramMotionEvent) {
		return false;
	}

	public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
		return this.mGestureDetector.onTouchEvent(paramMotionEvent);
	}

	public class CatologUpateMessageReceiver extends BroadcastReceiver {
		public void onReceive(Context paramContext, Intent paramIntent) {
			if (paramIntent.getIntExtra("BookID", -1) == BookCatologActivity.this.bookid) {
			    BookCatologActivity.this.handler
                .post(BookCatologActivity.this.runnable2chapterlist);
			}
		}
	}
}